//
//  LOLimagesCollectionViewCell.m
//  LOLXXbox
//
//  Created by mac on 16/3/1.
//  Copyright © 2016年 Grasstrio. All rights reserved.
//

#import "LOLimagesCollectionViewCell.h"
#define WIDTH [[UIScreen mainScreen]bounds].size.width
#define HEIGHT [[UIScreen mainScreen]bounds].size.height
#import <UIImageView+WebCache.h>
@interface LOLimagesCollectionViewCell ()<UIScrollViewAccessibilityDelegate>
@property(nonatomic,retain)UIScrollView *scrollView;
@property(nonatomic,retain)UIImageView *skinImageView;
@property(nonatomic,assign)BOOL isBigSize;
@property(nonatomic,assign)BOOL isImageAppear;
@property(nonatomic,assign)CGSize imageBigSize;
@property(nonatomic,assign)CGSize imageSmallSize;
@end
@implementation LOLimagesCollectionViewCell
-(instancetype)initWithFrame:(CGRect)frame
{
    if (self=[super initWithFrame:frame]) {
        [self createView];
    }
    return self;
}

-(void)createView
{
    self.scrollView = [[UIScrollView alloc]initWithFrame:[[UIScreen mainScreen] bounds]];
    self.scrollView.delegate =self;
    self.scrollView.backgroundColor = [UIColor blackColor];
    self.scrollView.bounces = NO;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    self.scrollView.showsVerticalScrollIndicator = NO;
    
    self.scrollView.contentSize = CGSizeMake(WIDTH, HEIGHT);
    [self.contentView addSubview:self.scrollView];
   
    
    //    self.scrollView.maximumZoomScale = 5.0;
    //    self.scrollView.minimumZoomScale = 1.0;
    
    self.skinImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, WIDTH/8,WIDTH/8)];
    self.skinImageView.center =self.scrollView.center;
    self.skinImageView.userInteractionEnabled = YES;
    [self.scrollView addSubview:self.skinImageView];

    
    UITapGestureRecognizer *signleTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(signleTapAction:)];
    signleTap.numberOfTapsRequired = 1;
    
    [self.scrollView addGestureRecognizer:signleTap];

    
    
    
    UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(doubleTapAction:)];
    doubleTap.numberOfTapsRequired = 2;
    
    [self.skinImageView addGestureRecognizer:doubleTap];

    
    
    
}

-(void)setIBModel:(LOLimagesModel *)IBModel
{
//    if (_IBModel != IBModel) {
//        [_IBModel release];
    _IBModel = IBModel;
//    }
    self.skinImage = nil;
    [self.skinImageView sd_setImageWithURL:[NSURL URLWithString:self.IBModel.bigImg] placeholderImage:[UIImage imageNamed:@"placeholder"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        self.skinImage = image;
        CGFloat height=self.contentView.frame.size.width*image.size.height/image.size.width;
        self.skinImageView.bounds=CGRectMake(0, 0, self.contentView.frame.size.width, height);
        self.skinImageView.center = self.scrollView.center;
        
        self.imageBigSize = CGSizeMake(image.size.width, image.size.height);
        self.imageSmallSize = CGSizeMake(self.contentView.frame.size.width, height);
        self.isImageAppear = YES;
        
    }];
    
    self.isBigSize = NO;
    self.scrollView.contentSize = CGSizeMake(WIDTH, HEIGHT);
    
    
}

-(void)signleTapAction:(UITapGestureRecognizer *)tap
{
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"hiddenView" object:nil];
    
}

-(void)doubleTapAction:(UITapGestureRecognizer *)tap
{
    
    
    if (self.isBigSize&&self.isImageAppear) {
        
        self.scrollView.contentSize = CGSizeMake(WIDTH, HEIGHT);
        self.scrollView.contentOffset = CGPointMake(0,0);
        self.skinImageView.center = self.scrollView.center;
        self.skinImageView.bounds = CGRectMake(0, 0, self.imageSmallSize.width, self.imageSmallSize.height);
        
        
    }
    else if(!self.isBigSize && self.isImageAppear)
    {
        
        self.scrollView.contentSize = self.imageBigSize;
        self.skinImageView.frame = CGRectMake(0, 0, self.imageBigSize.width, self.imageBigSize.height);
        CGPoint p =[tap locationInView:tap.view];
        
        
        CGFloat contentX =p.x*self.imageBigSize.width/self.imageSmallSize.width;
        CGFloat contentY =(p.y-self.center.y+self.imageSmallSize.height/2)*self.imageBigSize.height/self.imageSmallSize.height;
        
        if(contentX >self.imageBigSize.width-WIDTH)
        {
            
            contentX = self.imageBigSize.width-WIDTH;
            
        }
        if (contentY > self.imageBigSize.height - HEIGHT) {
            contentY = self.imageBigSize.height-HEIGHT;
            
        }
        
        
        self.scrollView.contentOffset = CGPointMake(contentX,contentY);
        
        
        
        
        
    }
    self.isBigSize = !self.isBigSize;
    
    
}



@end
